package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.entity.WmUser;
import com.heima.media.mapper.WmUserMapper;
import com.heima.media.service.IWmUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 自媒体用户信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-06-20
 */
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements IWmUserService {

    /**
     * 添加自媒体用户
     * @param User
     * @return
     */
    @Override
    public ResponseResult saveWmUser(WmUser User) {
        //判断承参数是否为空
        if (User==null || User.getApUserId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //构建查询条件
        LambdaQueryWrapper<WmUser> query = new LambdaQueryWrapper<>();
        //根据userId 查询,查看用户是否存在
        query.eq(WmUser::getApUserId,User.getApUserId());
        WmUser one = this.getOne(query);
        //如果存在,直接返回
        if (one!=null){
            return ResponseResult.okResult(one);
        }
        //如果不存在,创建用户
        User.setCreatedTime(new Date());
        this.save(User);
        return ResponseResult.okResult(User);
    }
}
